package org.rocky.consumer.service;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * @author Rocky
 * @date 2020/2/16 23:07
 */
@Component
@PropertySource("classpath:queues.properties")
public class ConsumeTTL {

    /**
     * 接收ttl消息
     * 无法使用converter--可能自身做了序列转换--或者消息对象无法被序列化
     * @param message Message类型数据
     * @throws IOException io
     */
    @RabbitHandler
    @RabbitListener(queues = "TTL_QUEUE", containerFactory="rabbitListenerContainerFactory"  )
    public void processTTL(Message message, Channel channel) throws IOException {

        System.out.printf("TTL Queue received msg : %s%n", new String(message.getBody()));
        //拒绝 进入死信队列
        channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        //重回 慎用
        //channel.basicPublish("TTL_EXCHANGE","rocky.ttl",new AMQP.BasicProperties(),message.getBody());

    }

}
